<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2021-08-30
 * Time: 09:07
 */

namespace app\admin\controller;


use app\admin\BaseController;

class QuestionController extends BaseController
{
    public function listsAction()
    {
        try {
            $params = [
                'where' => $this->_get_cond($this->get),
                'order' => [
                    'questionId' => 'desc'
                ]
            ];
            $questions = $this->app->questionService->getLists($params, $this->page, $this->pagesize);

            $data['lists'] = [];
            if (count($questions)) {
                foreach ($questions as $question) {
                    $data['lists'][] = [
                        'questionId' => $question->questionId,
                        'questionTitle' => $question->questionTitle,
                        'questionCategoryId' => $question->questionCategoryId,
                        'questionCategoryName' => $question->category()->value('categoryName'),
                        'questionCategoryKey' => $question->category()->value('categoryKey'),
                        'rightCredit' => $question->rightCredit,
                        'wrongCredit' => $question->wrongCredit,
                        'status' => $question->status,
                        'statusText' => $question->getStatusText(),
                        'createdAt' => $question->createdAt
                    ];
                }
            }
            $data['totalInfo'] = $this->app->utilService->getMeta($questions);

            return json(['code' => 200, 'msg' => '查询成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json(['code' => 500, 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function detailAction($questionId)
    {
        try {
            $question = $this->app->questionService->getDetail($questionId);

            $data = $question->toArray();
            $data['questionCategoryKey'] = $question->category()->value('categoryKey');
            $data['selects'] = $question->selects;

            return json(['code' => 200, 'msg' => '查询成功', 'data' => $data]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data' => []]);
        }
    }

    /**
     * @return \think\response\Json
     */
    public function addAction()
    {
        try {
            $params = $this->post;
            $this->app->questionService->create($params);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function updateAction($questionId)
    {
        try {
            $question = $this->app->questionService->getDetail($questionId);

            $this->app->questionService->update($question, $this->post);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function deleteAction($questionId)
    {
        try {
            $new = $this->app->questionService->getDetail($questionId);

            $this->app->questionService->delete($new);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function enableAction($questionId)
    {
        try {
            $new = $this->app->questionService->getDetail($questionId);

            $this->app->questionService->enable($new);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    /**
     * @param $id
     * @return \think\response\Json
     */
    public function disableAction($questionId)
    {
        try {
            $new = $this->app->questionService->getDetail($questionId);

            $this->app->questionService->disable($new);

            return json(['code' => 200, 'msg' => '操作成功', 'data'=> []]);
        } catch (\Exception $e) {
            return json(['code' => $e->getCode(), 'msg' => $e->getMessage(), 'data'=> []]);
        }
    }

    private function _get_cond($params)
    {
        $cond = [];

        if (isset($params['title']) && $params['title'] != '') {
            $cond[] = ['questionTitle', 'like', '%'.$params['title'].'%'];
        }

        if (isset($params['categoryId']) && $params['categoryId'] != '') {
            $cond[] = ['questionCategoryId', '=', $params['categoryId']];
        }

        return $cond;
    }
}